package com.ruoyi.generateTest.controller;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.file.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.generateTest.domain.GeneratedExam;
import com.ruoyi.generateTest.service.IGeneratedExamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.generateTest.utils.WordToExcelUtil.wordToExcelGeneral;

/**
 * 生成试题Controller
 * 
 * @author 经纬线
 * @date 2024-08-04
 */
@RestController
@RequestMapping("/generateTest/generate_exam")
public class GeneratedExamController extends BaseController
{
    @Autowired
    private IGeneratedExamService generatedExamService;

    /**
     * 查询生成试题列表
     */
    @PreAuthorize("@ss.hasPermi('list')")
    @GetMapping("/list")
    public TableDataInfo list(GeneratedExam generatedExam)
    {
        startPage();
        List<GeneratedExam> list = generatedExamService.selectGeneratedExamList(generatedExam);
        return getDataTable(list);
    }

    /**
     * 导出生成试题列表
     */
    @PreAuthorize("@ss.hasPermi('export')")
    @Log(title = "生成试题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GeneratedExam generatedExam)
    {
        List<GeneratedExam> list = generatedExamService.selectGeneratedExamList(generatedExam);
        ExcelUtil<GeneratedExam> util = new ExcelUtil<GeneratedExam>(GeneratedExam.class);
        util.exportExcel(response, list, "生成试题数据");
    }

    /**
     * 获取生成试题详细信息
     */
    @PreAuthorize("@ss.hasPermi('query')")
    @GetMapping(value = "/{examId}")
    public AjaxResult getInfo(@PathVariable("examId") Long examId)
    {
        return success(generatedExamService.selectGeneratedExamByExamId(examId));
    }

    /**
     * 新增生成试题
     */
    @PreAuthorize("@ss.hasPermi('add')")
    @Log(title = "生成试题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GeneratedExam generatedExam)
    {
        return toAjax(generatedExamService.insertGeneratedExam(generatedExam));
    }

    /**
     * 修改生成试题
     */
    @PreAuthorize("@ss.hasPermi('edit')")
    @Log(title = "生成试题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GeneratedExam generatedExam)
    {
        return toAjax(generatedExamService.updateGeneratedExam(generatedExam));
    }

    /**
     * 删除生成试题
     */
    @PreAuthorize("@ss.hasPermi('remove')")
    @Log(title = "生成试题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{examIds}")
    public AjaxResult remove(@PathVariable Long[] examIds)
    {
        return toAjax(generatedExamService.deleteGeneratedExamByExamIds(examIds));
    }


    /**
     * 下载试题文件
     * @param examId 试题id
     * @return 试题文件流
     * @throws IOException io异常
     */
    @Anonymous
    @GetMapping("/download/{examId}")
    public ResponseEntity<byte[]> downloadFile(@PathVariable("examId") Long examId) throws IOException {
        File file = new File(generatedExamService.selectGeneratedExamByExamId(examId).getFileName());
        byte[] data = Files.readAllBytes(file.toPath());
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=document.docx");
        return new ResponseEntity<>(data, headers, HttpStatus.OK);
    }






}
